from django.shortcuts import render
from django.http import HttpResponse
from django.db.utils import IntegrityError
from .models.user import User
from random import randrange
import time
import yagmail


# Create your views here.


def student_login(request):
    """
    登录
    :param request:
    :return:
    """
    if request.method == 'GET':
        return render(request, 'myapp/login.html')
    else:
        user = request.POST.get('user')
        password = request.POST.get('password')  # 对应input中的name
        # user = request.GET.get('user')  # 对应input中的name
        # password = request.GET.get('password')
        if user and password:
            result = User.objects.filter(user=user, password=password).count()
            if result > 0:
                return HttpResponse('登录成功')
            else:
                context = {
                    'info': '登录失败!\t',
                    'page': '登录',
                    'boole': '登录失败',
                }
                return render(request, 'myapp/jump.html', context=context)
        else:
            return render(request, 'myapp/login.html', {'hint': '用户名、密码不能为空'})


def registered(request):
    """
    注册
    :param request:
    :return: login.html
    """
    if request.method == 'POST':
        user = request.POST.get('user')
        password = request.POST.get('password')
        confirm = request.POST.get('confirm')
        email = request.POST.get('email')
        user_add = User.objects
        if password == confirm:
            try:
                result = user_add.create(user=user, password=password, email=email)
                if result.pk > 0:
                    context = {
                        'info': '注册成功！\t',
                        'page': '登录',
                        'boole': '成功',
                    }
                    return render(request, 'myapp/jump.html', context)
            except IntegrityError as e:
                context = {
                    'info': '注册失败！\t',
                    'page': '注册',
                    'boole': '注册失败',
                }
                return render(request, 'myapp/jump.html', context)
        else:
            return render(request, 'myapp/registered.html', {'confirm_hint': '两次密码输入不一致！'})
    elif request.method == 'GET':
        return render(request, 'myapp/registered.html')


def resetpassword(request):
    """
    重置密码
    :param request:
    :return:
    """
    if request.method == 'GET':
        return render(request, 'myapp/resetpassword.html')
    else:
        user = request.POST.get('user')
        password = request.POST.get('pwd1')
        get_time = float(request.POST.get('get_time'))
        now_time = time.mktime(time.localtime())
        get_msg = request.POST.get('verification')
        msg = request.POST.get('get_msg')
        print(get_time)
        print(password)
        if now_time - get_time < 180:
            if msg == get_msg:
                reset_pwd = User.objects.filter(user=user)
                result = reset_pwd.update(password=password)
                if result > 0:
                    context = {
                        'info': '重置密码成功！\t',
                        'page': '登录',
                        'boole': '成功',
                    }
                    return render(request, 'myapp/jump.html', context)
                else:
                    context = {
                        'info': '重置密码失败！\t',
                        'page': '重置密码',
                        'boole': '重置密码失败',
                    }
                    return render(request, 'myapp/jump.html', context)
            else:
                context = {
                    'user1': user,
                    'password1': password,
                    'show': '验证码输入错误！'
                }
                return render(request, 'myapp/resetpassword.html', context)
        else:
            context = {
                'user1': user,
                'password1': password,
                'show': '验证码超时，请重新获取'
            }
            return render(request, 'myapp/resetpassword.html', context)


def get_code(request):
    user = request.GET.get('user')
    email = request.GET.get('email')
    pwd1 = request.GET.get('password1')
    pwd2 = request.GET.get('password2')
    if user and email:
        count = User.objects.filter(user=user, email=email).count()
        if count > 0:
            msg = get_random()
            send_email(email, msg)
            datetime = time.mktime(time.localtime())
            context = {
                'user1': user,
                'email1': email,
                'password1': pwd1,
                'password2': pwd2,
                'datetime': datetime,
                'msg': msg,
            }
            return render(request, 'myapp/resetpassword.html', context)
        else:
            return render(request, 'myapp/resetpassword.html', {'show': '你输入的邮箱或用户名无效！'})
        pass
    else:
        return render(request, 'myapp/resetpassword.html', {'show': '用户名，密码，邮箱不能为空'})


def send_email(email, msg):
    mail = yagmail.SMTP(user='fengzhilaoling@163.com', password='WTDEWLQLRPCJHQTX', host='smtp.163.com')
    mail.send(to=email, subject='验证码', contents='学生管理平台验证码：' + msg + '\n验证码有效时间3分钟')


def get_random():
    msg = str(randrange(1000, 9999))
    return msg


def zhaohui(request):
    if request.method == 'GET':
        print(request.GET.get('user'))
        return render(request, 'myapp/zhaohui.html')
    else:
        return render(request, 'myapp/zhaohui.html')